package com.asa.stream;

import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class A {
	
	public static void main(String[] args) {
		
		int[] nums = {1,2,3};
		
		int sum = IntStream.of(nums).sum();//求和
		
		
		
		String str = "my name is asa";
		
		
		Stream.of(str.split(" ")).filter(s->s.length()>2).map(s->s.length()).forEach(System.out::println);
		
		
		Stream.of(str.split(" ")).flatMap(s->s.chars().boxed()).forEach(System.out::println);//foreach是终止操作，必须要有，不然不执行整个流

		System.out.println("========");
		
		Stream.of(str.split(" ")).peek(System.out::println).forEach(System.out::println);//peek也是遍历，但不是终止操作
		
		Random random = new Random();
		random.ints().limit(10).forEach(System.out::println);
		
		System.out.println("========");

		
		str.chars().parallel().forEach(i->System.out.print((char)i));//并行流
		System.out.println();
		str.chars().parallel().forEachOrdered(i->System.out.print((char)i));//保证次序
		System.out.println();

		List<String> collect = Stream.of(str.split(" ")).collect(Collectors.toList());
		System.out.println(collect);
		
		Optional<String> reduce = Stream.of(str.split(" ")).reduce((s1,s2)-> s1+"|"+s2);
		System.out.println(reduce.orElse(""));
		
		//计算单词总长度
		Integer reduce2 = Stream.of(str.split(" ")).map(s->s.length()).reduce(0,(s1,s2)-> s1+s2);
		System.out.println(reduce2);

		//找出最长的字符串单词
		Optional<String> max = Stream.of(str.split(" ")).max((s1,s2)->s1.length()-s2.length());
		System.out.println(max);
		
		
		
		
		
		
		
		
	}
	

}
